#include "ShadowLayer.h"
#include "MainLayer.h"


ShadowLayer::ShadowLayer(void)
{
	_textureRender = NULL;
	_spriteRender = NULL;
}


ShadowLayer::~ShadowLayer(void)
{
	CC_SAFE_RELEASE(_textureRender);
	CC_SAFE_RELEASE(_spriteRender);
}

void ShadowLayer::draw()
{
	float scaleNow = 0.8f;
	CCArray* listEnemy = dynamic_cast<MainLayer*>(getParent())->getEnemies();

	
	_textureRender->beginWithClear(0, 0, 0, 0);
	CCObject* it;
	CCARRAY_FOREACH(listEnemy, it)
	{
		Enemy* enemy = dynamic_cast<Enemy*>(it);
		float scale = enemy->getScale();
		enemy->setScale(scaleNow);
		enemy->visitShadow();
		enemy->setScale(scale);
	}

	// plane
	PlayerPlane* plane = dynamic_cast<MainLayer*>(getParent())->getMyPlant();
	float planeScale = plane->getScale();
	plane->setScale(scaleNow);
	plane->visit();
	plane->setScale(planeScale);
	// pet 1
	Pet* pet = dynamic_cast<MainLayer*>(getParent())->getPet1();
	planeScale = pet->getScale();
	pet->setScale(scaleNow);
	pet->visit();
	pet->setScale(planeScale);
	// pet 2
	pet = dynamic_cast<MainLayer*>(getParent())->getPet2();
	planeScale = pet->getScale();
	pet->setScale(scaleNow);
	pet->visit();
	pet->setScale(planeScale);
	_textureRender->end();

	_spriteRender->setTexture(_textureRender->getSprite()->getTexture());

// 	sprite->visit();
}

bool ShadowLayer::init()
{
	if (!CCNode::init())
		return false;

	_ccBlendFunc f = {GL_SRC_ALPHA_SATURATE, GL_ONE_MINUS_SRC_ALPHA};
	setBlendFunc(f);

	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCRenderTexture* texture = CCRenderTexture::create((int)size.width, (int)size.height);
	texture->setAnchorPoint(CCPointZero);
	texture->setPosition(ccp(0, 0));
	setTextureRender(texture);

	setSpriteRender(CCSprite::createWithTexture(texture->getSprite()->getTexture()));
	_spriteRender->setAnchorPoint(CCPointZero);
	_spriteRender->setFlipY(true);
	_spriteRender->setAnchorPoint(CCPointZero);
	_spriteRender->setBlendFunc(_blendFunc);
	_spriteRender->setOpacity(150);
	addChild(_spriteRender);

	return true;
}
